#include "stdafx.h"
#include "public.h"
#include "mainform.h"
#include "lotan.h"
#include "qinstance.h"
#include "analysis\sasform.h"
#include "controls\topbar.h"
#include "tools\screenshot.h"

MainForm::MainForm(QWidget *parent)
: SkinBaseWidget(parent)	
{
	setupUI();
}

void MainForm::setupUI()
{	
	assistantTitle = LoString(_T("KAssistant"));
	setWindowTitle(assistantTitle);
	setWindowIcon(QIcon(":assistant.png"));
	m_titleIcon.load(":assistant.png");
	ChangeMax();
	
	contentForm = NULL;
	contentArea = NULL;


	m_topbar = new TopBar;
	m_topbar->setFixedHeight(37);
	connect(m_topbar, SIGNAL(openSAS()), this, SLOT(slotOpenSAS()));

	QVBoxLayout *vLayout = new QVBoxLayout(this);
	vLayout->setSpacing(3);
	vLayout->setContentsMargins(7, 27, 7, 30);
	vLayout->addWidget(m_topbar, 0, Qt::AlignLeft | Qt::AlignTop);

	/*contentArea = new QScrollArea;
    contentArea->setWidget(contentForm);
    contentArea->setWidgetResizable(true);
    contentArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    contentArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    contentArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    contentArea->setMinimumSize(50, 50);

	QVBoxLayout *m_MainLayout = new QVBoxLayout(this);
	m_MainLayout->setContentsMargins(2, 66, 2, 2);

	m_MainLayout->setSpacing(0);
	m_MainLayout->addWidget(contentArea);*/

	connect(this, SIGNAL(quit()), this, SLOT(slotExit()));

	startHookCapital = false;
	capitalTimes = 0;
}

void MainForm::setParentTitle(QString title)
{
	setWindowTitle(assistantTitle + " -- " + title);
	repaint();
}

void MainForm::regionCamera()
{
	static ScreenShot *m_Shot = 0;
	if(!m_Shot)
	{
		m_Shot = new ScreenShot;
		connect(m_Shot, SIGNAL(shotWindow(QWidget *)), SLOT(slotFinishCamera(QWidget *)));
	}
	m_Shot->start();
}

void MainForm::slotFinishCamera(QWidget *wget)
{
	ScreenShot *m_Shot = (ScreenShot *)wget;
	if(m_Shot->isOK)
	{
		int w = m_Shot->pos2.x() - m_Shot->pos1.x();
		int h = m_Shot->pos2.y() - m_Shot->pos1.y();

		QPixmap originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), m_Shot->pos1.x(), m_Shot->pos1.y(), w, h);
		QImage shotImage = originalPixmap.toImage();


		QClipboard *cb = QApplication::clipboard();
		cb->setImage(shotImage);

		//cb->setPixmap(originalPixmap);
	}
	

	/*QString format = "png";
	QString initialPath = QDir::currentPath() + tr("/untitled.") + format;

	QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
		initialPath,
		tr("%1 Files (*.%2);;All Files (*)")
		.arg(format.toUpper())
		.arg(format));
	if (!fileName.isEmpty())
		originalPixmap.save(fileName, format.toAscii());*/
}

bool MainForm::winEvent(MSG *message, long *result)
{		
	WPARAM wParam = message->message;
	if(wParam == HM_USERKEYDOWN)
	{
		if(GetKeyState(VK_CONTROL) < 0)
		{
			//TRACE(_T("VK_CONTROL\n"));
			//return true;
		}
		if(GetKeyState(VK_SHIFT) < 0)
		{
			//TRACE(_T("VK_SHIFT\n"));
			//return true;
		}
		if(message->lParam == VK_ESCAPE)
		{
			//TRACE(_T("VK_ESCAPE\n"));
			//return true;
		}
		if(message->lParam == VK_CAPITAL)
		{
			runCaption();
			return true;
		}
		if(message->lParam < 'Z' && message->lParam > 'A')
		{
			if(GetKeyState(VK_CONTROL) < 0 &&
				GetKeyState(VK_MENU) < 0 &&
				message->lParam == 'E')
			{
				runAssistant();
				return true;
			}
			if(GetKeyState(VK_CONTROL) < 0 &&
				GetKeyState(VK_MENU) < 0 &&
				message->lParam == 'Q')
			{
				regionCamera();
				return true;
			}
		}
		return true;
	}
	return SkinBaseWidget::winEvent(message, result);
}

void MainForm::runAssistant()
{
	if(isVisible())
		hide();
	else
	{
		show();
	}
}

void MainForm::runCaption()
{
	if(!startHookCapital)
	{
		startHookCapital = true;
		QTimer::singleShot(1000, this, SLOT(slotStopHookCaptal()));
		capitalTimes = 1;
	}
	else
	{
		capitalTimes++;
		if(capitalTimes == 2)
		{
			ShellExecute(this->winId(), NULL, _T("D:/Contacts.txt"), NULL, NULL, SW_SHOWNORMAL);
			return;

			/*QString output = QString::fromAscii((const char *)&message->lParam, 1);
			QString outnum = QString::number((int)message->lParam);
			QString allout = "[" + outnum + "]" + output + "\n";
			TRACE((const wchar_t *)allout.data());*/

			/*QString program = "C:\\WINDOWS\\system32\\notepad.exe";
			QStringList arguments;
			QString execute = QString("D:/Contacts.txt").arg("");
			arguments << execute;

			QProcess *myProcess = new QProcess;
			myProcess->start(program, arguments);

			TRACE(_T("VK_CAPITAL: DO MY THING.\n"));*/


			/*QString program = "C:/Program Files/Internet Explorer/iexplore";
			QStringList arguments;
			QString execute = QString("http://www.baidu.com/s?wd=%1&cl=3").arg("");
			arguments << execute;

			QProcess *myProcess = new QProcess;
			myProcess->start(program, arguments);

			TRACE(_T("VK_CAPITAL: DO MY THING.\n"));*/
			//todo
			//StopHookCaptal();
		}
	}
}


void MainForm::resizeEvent(QResizeEvent *e)
{
	if(contentForm && contentForm->isVisible())
		contentForm->setGeometry(2, 66, width() - 4, height() - 68);
	SkinBaseWidget::resizeEvent(e);
}


void MainForm::slotExit()
{	
	hide();
	return;
	QInstance::Instance()->isExit = true;
	QApplication::instance()->quit();
}

void MainForm::slotStopHookCaptal()
{
	startHookCapital = false;
	capitalTimes = 0;
}

void MainForm::slotOpenSAS()
{
	if(!contentForm)
	{
		contentForm = new SASForm(this);
		contentForm->hide();
	}
	if(contentForm->isVisible())
		contentForm->hide();
	else
	{	
		contentForm->show();
		contentForm->setGeometry(3, 66, width() - 6, height() - 69);
	}
}